package top.wineye.spring.dependency.lookup;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.LifecycleProcessor;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.core.env.Environment;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;


/**
 * @see org.springframework.context.annotation.AnnotationConfigUtils
 */
public class ContainerBuildInDependencies {


    public static void main(String[] args) {

        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(ContainerBuildInDependencies.class);
        /*
            对于AbstractApplicationContext 而言 内建可查找依赖有
                * environment                   Environment                 外部化配置和Profiles
                * systemProperties              java.util.Properties        Java 系统属性
                * systemEnvironment             java.util.Map               操作系统环境变量
                * messageSource                 MessageSource               国际化文案
                * lifecycleProcessor            LifecycleProcessor          Lifecycle Bean 处理器
                * applicationEventMulticaster   ApplicationEventMulticaster 时间广播

         */

        System.out.println("this is an empty container...");

        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        System.out.println("all bean definition names[" + beanDefinitionNames.length +
                "] :" + Arrays.toString(beanDefinitionNames));

        List<String> beanDefinitionClassNames = Arrays.stream(beanDefinitionNames)
                .map(context::getBeanDefinition)
                .map(BeanDefinition::getBeanClassName)
                .collect(Collectors.toList());
        System.out.println("all bean definition class names: " + beanDefinitionClassNames);

        Map<String, Object> beansOfType = context.getBeansOfType(Object.class);
        System.out.println("all bean names ["+ beansOfType.size() +"]: "+ beansOfType.keySet());

        /*
            bean definition 有 6 个
            但得到的 bean 却有 12 个
            这就说明，有 6 个正好不是由 bean definition 产生的

            bean definition 中，5 个是容器自己定义的 包含
                * 4 个 BeanFactoryPostProcessor
                * 1 个 EventListenerFactory

         */

        Environment environment = context.getBean("environment", Environment.class);
        System.out.println(environment);

        Properties systemProperties = context.getBean("systemProperties", Properties.class);
        System.out.println(systemProperties);

        Map systemEnvironment = context.getBean("systemEnvironment", Map.class);
        System.out.println(systemEnvironment);

        MessageSource messageSource = context.getBean("messageSource", MessageSource.class);
        System.out.println("messageSource: " + messageSource);

        LifecycleProcessor lifecycleProcessor =
                context.getBean("lifecycleProcessor", LifecycleProcessor.class);
        System.out.println("lifecycleProcessor: " + lifecycleProcessor);

        ApplicationEventMulticaster applicationEventMulticaster =
                context.getBean("applicationEventMulticaster", ApplicationEventMulticaster.class);
        System.out.println("applicationEventMulticaster: " + applicationEventMulticaster);


        context.close();

    }

}
